﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

namespace RuEn
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
    public class RegExRule : RuleBase
    {
        private string _expression = string.Empty;
        private string _errorMessage = "The input doesn't match the defined criteria";

        public RegExRule(string expression)
        {
            this._expression = expression;
        }

        public RegExRule(string expression, string ErrorMessage)
        {
            this._expression = expression;
            this._errorMessage = ErrorMessage;
        }

        public string Expression
        {
            get { return _expression; }
        }

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public bool Validate(string value, string Expression, string ErrorMessage)
        {            
            if (!string.IsNullOrEmpty(value))
            {
                if (!string.IsNullOrEmpty(Expression))
                {
                    Regex validationExp = new Regex(Expression);
                    if (!validationExp.IsMatch(value))
                    {
                        BrokenRules.Add(ErrorMessage);
                    }
                }
            }
            return true;
        }
    }
}
